package com.ruoyi.common.core.utils;

import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;


public class RobotUtils {

    public static void main(String[] args) throws IOException {
//        Scanner scan = new Scanner(System.in);
//        System.out.println("请输入要发送的消息内容：");
//        String content = scan.nextLine();
        //普通文字
//        sendText("你好");
        //带艾特人
//        sendQuan("你好","17375746121,17375746121");

        sendYangsi("你好","17375746121,17375746121");
    }

    //普通文字
    public static String sendText(String content)  {

        content =   "{\n" +
                "   \"msgtype\": \"text\",\n" +
                "   \"text\": {\n" +
                "    \"content\": \"" + content + "\"\n" +
                "  }\n" +
                "}";

        try {
            return send(content);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    //带艾特人!电话号用,隔开{14567,184156,256}
    public static String sendQuanAll(String content)  {

        content =   "{\n" +
                "   \"msgtype\": \"text\",\n" +
                "   \"text\": {\n" +
                "    \"content\": \"" + content + "\"\n" +","+
                "     \"mentioned_mobile_list\":[\"@all\"]"+
//                "     \"mentioned_mobile_list\":["+telephone+"]"+
                "  }\n" +
                "}";
        System.out.println(content);
//            return content1;
        try {
            return send(content);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    public static String sendQuan(String content,String telephone)  {

        content =   "{\n" +
                "   \"msgtype\": \"text\",\n" +
                "   \"text\": {\n" +
                "    \"content\": \"" + content + "\"\n" +","+
//                "     \"mentioned_mobile_list\":[\"17375746121\",\"@all\"]"+
                "     \"mentioned_mobile_list\":["+telephone+"]"+
                "  }\n" +
                "}";
        System.out.println(content);
//            return content1;
        try {
            return send(content);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    //添加样式
    public static String sendYangsi(String content,String telephone){
        String aa = "<font color=\\\"warning\\\">用户反馈</font>";
        String aa1 = "请相关同事注意。\n";
        content =   "{\n" +
                "   \"msgtype\": \"markdown\",\n" +
                "   \"markdown\": {\n" +
                "    \"content\": \"" + aa + "\"\n" +","+
//                "     \"mentioned_mobile_list\":[\"17375746121\",\"@all\"]"+
//                "     \"mentioned_mobile_list\":["+telephone+"]"+
                "  }\n" +
                "}";
        System.out.println(content);
//            return content1;
        try {
            return send(content);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    //添加样式
    public static String sendTupian(String content,String telephone) throws IOException {
        String aa = "<font color=\\\"warning\\\">用户反馈</font>";
        String aa1 = "请相关同事注意。\n";
        content =   "{\n" +
                "   \"msgtype\": \"markdown\",\n" +
                "   \"markdown\": {\n" +
                "    \"content\": \"" + aa + "\"\n" +","+
//                "     \"mentioned_mobile_list\":[\"17375746121\",\"@all\"]"+
//                "     \"mentioned_mobile_list\":["+telephone+"]"+
                "  }\n" +
                "}";
        System.out.println(content);
//            return content1;
        return send(content);
    }




    public static String send(String textMsg) throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();//实例化对象
//        HttpPost httpPost = new HttpPost("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=33d69c01-d962-478b-8945-288e00e2abf2");//url地址
        HttpPost httpPost = new HttpPost("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=971c724f-a322-4f2a-9ad2-388990d1b8ac");//url地址
        httpPost.addHeader("Content-Type", "application/json; charset=utf-8");//发送消息的格式;
        StringEntity se = new StringEntity(textMsg, "utf-8");//编码转换
        httpPost.setEntity(se);
        CloseableHttpResponse response = httpClient.execute(httpPost);
        //发送成功接收返回值
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            String result = EntityUtils.toString(response.getEntity(), "utf-8");
            System.out.println("发送微信机器人消息成功 " + result);
            return result;
        } else {
            System.out.println("发送微信机器人消息失败");
        }
        // 关闭
        httpClient.close();
        response.close();
        return "发送微信机器人消息失败";
    }

}